package com.example.demo.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建执行线程方式三：实现Callable接口
 * 相比较于Runable接口的方式，可以有返回值，并且可以抛出异常
 */
public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadDemoCall call = new ThreadDemoCall();
        //执行 需要 FuturTask支持，来接受运算结果
        FutureTask<Integer> result = new FutureTask<>(call);
        new Thread(result).start();
    // 接受结果
        System.out.println(result.get());
    }
}

class ThreadDemoCall implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int sum = 10;
        for (int i = 0; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }
}
